package com.example.myapplication.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.hatom.cameraxhelp.fragment.dialog.FaceChooseDialog;
import com.hatom.cameraxhelp.fragment.dialog.FaceMultiChooseDialog;
import com.hatom.cameraxhelp.utils.Base64Utils;


/**
 * @author tangxianfeng
 * 自己处理预览数据返回的页面
 * 可参考扩展实现
 */
public class TestDialogShowFragment extends Fragment {


    private FaceChooseDialog faceChooseDialog;
    private FaceMultiChooseDialog faceMultiChooseDialog;
    private ImageView result_img;

    public TestDialogShowFragment() {

    }

    public static TestDialogShowFragment newInstance() {
        TestDialogShowFragment fragment = new TestDialogShowFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.camerax_fragment_dialogtest_camera, container, false);
        initView(view);
        return view;
    }

    private void initView(View view){
        result_img=view.findViewById(R.id.result_img);
        view.findViewById(R.id.cameradialog_test_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (faceChooseDialog==null){
                    faceChooseDialog=new FaceChooseDialog();
                    faceChooseDialog.setGetFace(new FaceChooseDialog.GetFace() {
                        @Override
                        public void getPhoto(String base64) {
                            Bitmap resultbitmap= Base64Utils.base64ToBitmap(base64);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    result_img.setImageBitmap(resultbitmap);
                                }
                            });
                        }

                        @Override
                        public void error(String msg) {
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(requireActivity(),msg,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    });
                }
                faceChooseDialog.show(requireFragmentManager(),"faceChooseDialog");
            }
        });

        view.findViewById(R.id.cameradialog_multi_test_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (faceMultiChooseDialog==null){
                    faceMultiChooseDialog=new FaceMultiChooseDialog();
                    faceMultiChooseDialog.setGetFace(new FaceMultiChooseDialog.GetFace() {
                        @Override
                        public void getPhoto(String base64) {
                            Log.e("TEST","getPhoto"+base64);
                            Bitmap resultbitmap= Base64Utils.base64ToBitmap(base64);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    result_img.setImageBitmap(resultbitmap);
                                }
                            });
                        }

                        @Override
                        public void error(String msg) {
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(requireActivity(),msg,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    });
                }
                faceMultiChooseDialog.show(requireFragmentManager(),"faceMultiChooseDialog");
            }
        });
    }
}